"use strict";
var _lessonid = null;
var _phase = null;
$(function () {
    var params = getParamsFromUrl();
    _lessonid = params[0];
    _phase = params[1];

    getLessonHttp(_lessonid, _phase);
});

function getParamsFromUrl() {
    var url = decodeURI(location.href);
    var urlParmListStr = url.substr(url.indexOf('?') + 1, url.length);
    var params = urlParmListStr.split("&");
    var param1 = params[0].split("=")[1];
    var param2 = params[1].split("=")[1];
    return [param1, param2];
}

function fillLessonLearnCont(theLesson) {

    $("#lessonTitle").html(theLesson.lessonname);
    $("#lessonContent").html(theLesson.description);
    $("#lessonImg").attr('src', theLesson.image);
    $("#lessonTeacherName").append(theLesson.ownername);
    $("#lessonPhase").append(theLesson.phase);

    var labs = theLesson.labs;
    var labsHtml = "";

  switch(theLesson.currentnb){
      case 1:$('#onlineMap').hide();$('#onlineCode').hide();$('#themeMap').hide();break;//桌面制图
      case 2:$('#onlineCode').hide();$('#themeMap').hide();break;//在线制图
      case 3:$('#onlineMap').hide();$('#themeMap').hide();break;//在线开发
      case 4:$('#onlineMap').hide();$('#onlineCode').hide();break;//专题制图
      case 99:$('#onlineMap').hide();$('#onlineCode').hide();$('#themeMap').hide();break;//其他
      default:break;
  }
    $("#lessonLabsUl").html(labsHtml);
    // 获取url
    var isOtherUrl=false;
    var geturl=function(u){
        if(/==$/.test(u)){
            isOtherUrl=false;
            return "<iframe height=498 width=510 src=\\'http://player.youku.com/embed/"+u+"\\' frameborder=0 allowfullscreen></iframe>";
        }else if(/^<iframe/.test(u)){
            isOtherUrl=false;
            return u.replace(/"/g, '\\"').replace(/'/g, "\\'");
        }else if ( /^http:\/\//.test(u) || /^https:\/\//.test(u) || /^<embed/.test(u) ) {
            if (/youku.com/.test(u)) {
                var vid = u.split("show/id_")[1];
                if (vid) {
                    isOtherUrl=false;
                    return "<iframe height=498 width=510 src=\\'http://player.youku.com/embed/"+vid.split(".")[0]+"\\' frameborder=0 allowfullscreen></iframe>";
                }else{
                    var vvid= u.split("sid/")[1];
                    if(vvid){
                        isOtherUrl=false;
                        return "<iframe height=498 width=510 src=\\'http://player.youku.com/embed/"+vvid.split("/v")[0]+"\\' frameborder=0 allowfullscreen></iframe>";
                    }else{
                        isOtherUrl = true;
                        return u;
                    }
                }
            }else{
                isOtherUrl=true;
                return u;
            }
        }else{
            isOtherUrl=true;
            return u;
        }
    };
    for (var i in labs) {
        var thelab = labs[i];
        var li = $("<li class=\"media\"><h4>" + thelab.name + "</h4><p>" + thelab.description + "</p></li>");

        if (thelab.labresources.length > 0) {
            li.append("<table  class=\"table table-hover table-bordered  tabstyle\"><thead><tr><th>" + langThis("lab_labdata") + "</th><th>" + langThis("lab_labmethd") + "</th><th>"+langThis("nv_ved")+"</th></tr></thead><tbody   class=\"itemTbody\"><tr><td class='dataTd'></td><td class='docTd'></td><td class='vedTd'></td></tr></tbody></table>");
            var td_data = li.find('td.dataTd');
            var td_doc = li.find('td.docTd');
            var td_ved=li.find('td.vedTd');
            for (var j in thelab.labresources) {
                var theSource = thelab.labresources[j];
                if (theSource.type == "data") {
                    td_data.append("<a  role=\"button\" class=\"btn btn-link\" href=\"" + theSource.url + "\" target=\"_balnk\">" + theSource.name + "</a>");
                    td_data.append("<span>/</span>");
                } else if(theSource.type == "doc") {
                    td_doc.append("<a  role=\"button\" class=\"btn btn-link\" href=\"" + theSource.url + "\" target=\"_balnk\">" + theSource.name + "</a>");
                    td_doc.append("<span>/</span>");
                }
                else{//video
                  //  var _url=geturl(theSource.url);
                 //   if (isOtherUrl) {
                //   td_ved.append("<a class=\"btn btn-link\" href="+theSource.url+" target='_blank'>" + theSource.name + "</a><span>/</span>");
                //   } else {
                        td_ved.append("<a  role=\"button\" class=\"btn btn-link\" onclick=\"playVedieo('" + theSource.url + "')\">" + theSource.name + "</a>");
                        td_ved.append("<span>/</span>");
                 //  }
                }
            }
            td_data.find('span').last().remove();
            td_doc.find('span').last().remove();
            td_ved.find('span').last().remove();
        }
        $("#lessonLabsUl").append(li);
    }
}
function playVedieo(sourceurl){
var divStr="<div id=\"youkuplayer\" style=\"width:100%;height:100%;overflow:hidden\"></div>";
layer.open({
  type: 1,
  content: divStr,//这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
 shade:0,
 area: ['650px','550px'],
 scrollbar: false,
 success: function(layero, index){
     var player = new YKU.Player('youkuplayer',{

        styleid: '0',

        client_id: '7ad45d534836ba07',

        vid: sourceurl,

        newPlayer: true});

  }
});
}
function getLessonHttp(id, phase) {
    var _url = "publishlessons/" + id + "/" + phase + "/lessoninfo";
    $.ajax({
        url: _url,
        type: "GET",
        data: {},
        dataType: "json",
        success: function (data) {
            if (data != undefined && data != null) {

                fillLessonLearnCont(data);


            } else {

            }

        },
        error: function (event) {


        }
    });

}